// pages/member/myOrder/express/express.js
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    companyShow: false,
    form: [],
    express: [],
    return_address_list: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.refund_id) {
      this.setData({
        refund_id: options.refund_id,
      });
    }
    this._initData();
    this.getReturnGoods();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  scanCode(e){
    this.selectIndex = e.currentTarget.dataset.index;
    wx.scanCode({
      onlyFromCamera: true,
      success:(res)=>{
        this.setData({
          ['form[' + this.selectIndex + '].express_sn']:res.result
        });
      }
    });
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  _initData() {
    this.setData({
      companyShow: false,
      express: [],
    });
  },
  companyShowbtn: function (e) {
    this.selectIndex = e.currentTarget.dataset.index;
    this.setData({
      companyShow: true,
    });
  },
  expressclear(e) {
    this.selectIndex = e.currentTarget.dataset.index;
    this.setData({
      ['form[' + this.selectIndex + '].express_sn']: "",
    });
  },
  //快递单号输入
  expressbtn(e) {
    this.selectIndex = e.currentTarget.dataset.index;
    let express_sn = e.detail;
    this.setData({
      ['form[' + this.selectIndex + '].express_sn']: express_sn,
    });
  },
  //关闭快递选择
  companyShowClose: function () {
    this.setData({
      companyShow: false,
    });
  },
  _getExpressInfo(plugin_id) {
    let url = "dispatch.express-list";
    if (plugin_id == 44) {
      url = "plugin.jd-supply.frontend.dispatch.express.express-company";
    }
    let urlStr = app.getNetAddresss(url);
    app._getNetWork({
      url: urlStr,
      success: (resdata)=> {
        let res = resdata.data;
        if (res.result == 1) {
          let myData = res.data;
          this.setData({
            express: myData.express_companies,
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  submitInfo() {
    let urlStr = app.getNetAddresss("refund.operation.batch-send");
    app._postNetWork({
      url: urlStr,
      data: {
        refund_id: this.data.refund_id,
        send_info: this.data.form
      },
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          wx.showToast({
            title: res.msg,
            icon: "none",
            duration: 1500,
            success: function () {
              setTimeout(function () {
                //要延时执行的代码
                wx.navigateBack({
                  delta: 1,
                });
              }, 1500); //延迟时间
            },
          });
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1500,
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  companyConfirm(e) {
    let item = e.currentTarget.dataset.item;
    this.setData({
      ['form[' + this.selectIndex + '].express_company_name']: item.name,
      ['form[' + this.selectIndex + '].express_company_code']: item.value,
      companyShow: false,
    });
    console.log(this.data.form)
  },
  getReturnGoods() {
    let urlStr = app.getNetAddresss("refund.detail");
    app._getNetWork({
      url: urlStr,
      data: { refund_id: this.data.refund_id },
      success: (resdata)=> {
        let res = resdata.data;
        if (res.result == 1) {
          let return_address_list = res.data.return_address_list
          return_address_list.forEach((e,i) => {
            this.data.form.push({
              "express_sn": "",  //物流编号
              "express_company_code": "", // 物流公司标识
              "order_goods_ids": [], //该快递包裹下订单商品ID数组
              "address": e.province_name + e.city_name + e.district_name + e.street_name + e.address, //发送快递的地址
              "contact": e.contact, //快递收货人名称
              "mobile": e.mobile, //联系方式
              "plane": e.plane,  //非必填
              "images":"",//非必填
              "express_company_name":"",//非必填
            });
            e.refund_order_goods.forEach(g => {
              this.data.form[i].order_goods_ids.push(g.order_goods_id);
            });
          });
          this.setData({
            return_address_list,
            form: this.data.form
          });
          this._getExpressInfo(res.data.order.plugin_id);
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
});
